<template>
  <div>
    <van-list v-model="loading" :error.sync="error" error-text="请求失败，点击重新加载" :finished="finished" finished-text="没有更多了"
      @load="onLoad">
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResults } from '@/api/search.js'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_spage: 10,
      error: false
    }
  },
  props: {
    value: {
      type: String,
      require: true
    }
  },
  methods: {
    async onLoad () {
      // 1.发送请求获取数据
      try {
        const res = await getSearchResults({
          page: this.page,
          per_page: this.per_page,
          q: this.value
        })
        console.log(res)
        // 2.保存数据
        this.list.push(...res.data.data.results)
        console.log(this.list)
        // 3.当前这一次的加载状态结束
        this.loading = false
        // 4.数据全部加载完成
        if (this.list.length === res.data.data.total_count) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
    }
  },
  computed: {},
  watch: {
  },
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
